<?php

namespace app\command;

use app\admin\service\WechatService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\facade\Db;

/**
 * 微信媒资批量授权小程序
 * Class WxMediaBatchAuthTask
 * @package app\command
 */
class WxMediaBatchAuthTask extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('wxMediaBatchAuthTask')
            ->addArgument('appIds', Argument::REQUIRED, "appIds")
            ->setDescription('the wxMediaBatchAuthTask command');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('wxMediaBatchAuthTask begin');
        $appids = trim($input->getArgument('appIds'));
        $appids = explode(',', $appids);
        $applets = Db::name('applet')->whereIn('app_id', $appids)->where('type',1)->column('app_id', 'id');
        if (empty($applets)) {
            $output->writeln('please enter the correct app_id');
            return;
        }

        $where = [['v.is_delete', '=', 0], ['v.is_copy', '=', 0], ['v.original_id', '=', 0], ['v.drama_id', '>', 0]];
        $videos = Db::name('videos')->alias('v')
            ->join('third_videos t', 't.video_id = v.id and platform = 1', 'left')
            ->field('t.id,t.applet_ids,v.id as video_id,v.drama_id')
            ->where($where)
            ->select()
            ->toArray();

        $service = new WechatService();
        $time = time();
        foreach ($videos as $video) {
            $origin_app_ids = !empty($video['applet_ids']) ? json_decode($video['applet_ids'], true) : [];
            $app_ids = array_keys($applets);

            $diff = array_diff($app_ids, $origin_app_ids);
            if (empty($diff)) {
                continue;
            }

            $app_keys = [];
            foreach ($diff as $aid) {
                $app_keys[] = $applets[$aid];
            }
            $app_keys = array_filter($app_keys);

            $result = $service->shortPlayAuthorize($video['drama_id'], $app_keys);
            if (empty($result)) {
                continue;
            }

            $newAids = [];
            foreach ($applets as $k => $app_id) {
                if (in_array($app_id, $result)) {
                    $newAids[] = $k;
                }
            }

            $updateData = [
                'is_authorize' => 1,
                'applet_ids' => json_encode(array_merge($origin_app_ids, $newAids)),
                'updated_at' => $time
            ];

            if (empty($video['id'])) {
                $res = Db::name('third_videos')->insert(array_merge([
                    'video_id' => $video['video_id'],
                    'platform' => 1,
                    'album_id' => $video['drama_id'],
                    'created_at' => $time
                ], $updateData));
            } else {
                $res = Db::name('third_videos')->where('id', $video['id'])->update($updateData);
            }

            $str = 'video_id:' . $video['video_id'] . ',授权' . ($res ? '成功' : '失败');
            $output->writeln($str);
        }

        $output->writeln('wxMediaBatchAuthTask end');
    }
}